home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / tpwgray.com / TPWGRAYS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-05-31  |  5.3 KB  |  197 lines

  1. {TPWGRAYS.PAS - a TPW/ObjectVision version of Grays.C (C) Copyright Charles Petzold 1990}
  2. {Adaption D.Overmyer}
  3. program TPWGRAYS;
  4. {$R TPWGRAYS.RES}
  5.  
  6. uses WinTypes, WinProcs, WObjects, Strings;
  7.  
  8. const
  9.   PG_Name =  'TPWGRAYS';
  10.   cm_Dithered = 1;
  11.   cm_PaletteRGB = 2;
  12.   cm_PaletteIndex = 3;
  13. {*****************************************************************}
  14. {T Y P E S }
  15. {*****************************************************************}
  16. type
  17.  
  18. { TPGApplication, a TApplication descendant }
  19.   TPGApplication = object(TApplication)
  20.     procedure InitMainWindow; virtual;
  21.   end;
  22.  
  23. { TPGWindow, a TWindow descendant }
  24.   PPGWindow = ^TPGWindow;
  25.   TPGWindow = object(TWindow)
  26.     hPal :HPalette;
  27.     cxClient,cyClient:Word;
  28.     wDisplay:Word;
  29.     constructor Init(AParent:PWindowsObject;ATitle: PChar);
  30.     destructor Done; virtual;
  31.     function  Min(i,j:Integer):integer;virtual;
  32.     procedure CMDithered(var msg:TMessage);virtual cm_First+cm_Dithered;
  33.     procedure CMPaletteRGB(var Msg:TMessage);virtual cm_First+cm_PaletteRGB;
  34.     procedure CMPaletteIndex(var Msg:TMessage);virtual cm_First+cm_PaletteIndex;
  35.     procedure UpdateMenu(var Msg:TMessage);virtual;
  36.     procedure Paint(PaintDC:HDC;var PaintInfo:TPaintStruct);virtual;
  37.     procedure WMQueryNewPalette(var Msg:TMessage);virtual wm_First+wm_QueryNewPalette;
  38.     procedure WMPaletteChanged(var Msg:TMessage);virtual wm_First+wm_PaletteChanged;
  39.     procedure WMSize(var Msg:TMessage);virtual wm_First+wm_Size;
  40.   end;
  41.  
  42. {*****************************************************************}
  43. {M E T H O D S }
  44. {*****************************************************************}
  45.  
  46. { Construct the TPGApp's MainWindow of type TPGWindow }
  47. procedure TPGApplication.InitMainWindow;
  48. begin
  49.   MainWindow := New(PPGWindow, Init(nil,PG_name));
  50. end;
  51.  
  52. constructor TPGWindow.Init(AParent:PWindowsObject;ATitle: PChar);
  53. var
  54.   plp :PLogPalette;
  55.   i :Integer;
  56.   rc :TRect;
  57.   nGrayLevel:Byte;
  58.   size  : Word;
  59. begin
  60. {$R-}   {Must disable range checking for plp due to TLogPalette declaration}
  61.   TWindow.Init(nil, ATitle);
  62.   Attr.Style := Attr.Style or ws_VScroll or ws_HScroll;
  63.   Attr.Menu := LoadMenu(HInstance, 'PG_Menu');
  64.   size := sizeof(TLogPalette)+64*sizeof(TPaletteEntry);
  65.   plp := MemAlloc(size);
  66.   plp^.palVersion := $0300;
  67.   plp^.palNumEntries := 65;
  68.   for i := 0 to  64 do
  69.           begin
  70.       nGrayLevel := min(255,4*i);
  71.       plp^.palPalEntry[i].peRed := nGrayLevel;
  72.       plp^.palPalEntry[i].peGreen := nGrayLevel;
  73.       plp^.palPalEntry[i].peBlue := nGrayLevel;
  74.       plp^.palPalEntry[i].peFlags := 0;
  75.   end;
  76.   hPal := CreatePalette(plp^);
  77.   Freemem(plp,size);
  78.   wDisplay := cm_Dithered;
  79.   {$R+}
  80. end;
  81.  
  82. destructor TPGWindow.Done;
  83. begin
  84.   DeleteObject(HPal);
  85.   TWindow.Done;
  86. end;
  87.  
  88. function TPGWindow.Min(i,j:integer):integer;
  89. begin
  90.     min := j;
  91.     if i<j then min := i;
  92. end;
  93.  
  94. procedure TPGWindow.UpdateMenu(var Msg: TMessage);
  95. begin
  96.     CheckMenuItem(Attr.Menu,wDisplay,MF_UnChecked);
  97.    wDisplay := Msg.wParam;
  98.    CheckMenuItem(Attr.Menu,wdisplay,MF_Checked);
  99.    InvalidateRect(HWindow,nil,True);
  100. end;
  101.  
  102. procedure TPGWindow.CMDithered(var Msg: TMessage);
  103. begin
  104.     UpdateMenu(Msg);
  105. end;
  106.  
  107. procedure TPGWindow.CMPaletteRGB(var Msg: TMessage);
  108. begin
  109.     UpdateMenu(Msg);
  110. end;
  111.  
  112. procedure TPGWindow.CMPaletteIndex(var Msg: TMessage);
  113. begin
  114.     UpdateMenu(Msg);
  115. end;
  116.  
  117. procedure TPGWindow.WMSize(var Msg:TMessage);
  118. begin
  119.     cxClient := Msg.LParamLo;
  120.    cyClient := Msg.LParamHi;
  121. end;
  122.  
  123. procedure TPGWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  124. var
  125.     i : Integer;
  126.    aBrush : HBrush;
  127.    rc : TRect;
  128.    nGrayLevel:word;
  129.    hOldPal : HPalette;
  130. begin
  131. if (wDisplay <> cm_Dithered) then
  132.     begin
  133.    hOldPal :=SelectPalette(PaintDC,hPal,False);
  134.    RealizePalette(PaintDC);
  135.     end;
  136. for i := 0 to 64 do
  137.     begin
  138.    rc.left := Integer(i * cxClient div 65);
  139.    rc.top := 0;
  140.    rc.right := Integer((i+1) * cxClient div 65);
  141.    rc.bottom := cyClient;
  142.    nGrayLevel := min(255,4*i);
  143.    if wDisplay = cm_Dithered then
  144.        aBrush := CreateSolidBrush(RGB(nGrayLevel,nGrayLevel,nGrayLevel));
  145.    if wDisplay = cm_PaletteRGB then
  146.        aBrush := CreateSolidBrush(PaletteRGB(nGrayLevel,nGrayLevel,nGrayLevel));
  147.    if wDisplay = cm_PaletteIndex then
  148.        aBrush := CreateSolidBrush(PaletteIndex(i));
  149.    FillRect(PaintDC,rc,aBrush);
  150.    DeleteObject(aBrush);
  151.    end;
  152. if (wDisplay <> cm_Dithered) then SelectPalette(PaintDC,hOldPal,False);
  153. end;
  154.  
  155. procedure TPGWindow.WMQueryNewPalette(var Msg: TMessage);
  156. var
  157.     ahDC :HDC;
  158. begin
  159. ahDC := GetDC(HWindow);
  160. SelectPalette(ahDC,hPal,False);
  161. if (RealizePalette(ahDC) > 0) then
  162.      begin
  163.    ReleaseDC(HWindow,ahDC);
  164.    InvalidateRect(HWindow,Nil,False)
  165.    end
  166. else
  167.    ReleaseDC(HWindow,ahDC);
  168. end;
  169.  
  170. procedure TPGWindow.WMPaletteChanged(var Msg: TMessage);
  171. var
  172.     ahDC : HDC;
  173. begin
  174. if (Msg.wParam <> HWindow) then
  175.     begin
  176.    ahDC := GetDC(HWindow);
  177.    SelectPalette(ahDC,hPal,False);
  178.    if (RealizePalette(ahDC) > 0) then
  179.        InvalidateRect(HWindow,nil,False);
  180.    ReleaseDC(HWindow,ahDC);
  181.    end;
  182. end;
  183.  
  184. {*****************************************************************}
  185. {M A I N    L I N E }
  186. {*****************************************************************}
  187. { Declare a variable of type TPGApp }
  188. var
  189.   PGApp: TPGApplication;
  190.  
  191. { Run the PGApp }
  192. begin
  193.   PGApp.Init('TPWGRAYS');
  194.   PGApp.Run;
  195.   PGApp.Done;
  196. end.
  197.